import axios from "axios";

export let baseUrl = "/service/youquan"; //生产
if (process.env.NODE_ENV !== "production") {
    //  开发 youquan   api生产
    baseUrl = "/youquan";
    //baseUrl = "/api";
}

const http = axios.create({
    baseURL: baseUrl,
});

export interface ResProps {
    success?: boolean;
    data?: any;
}

interface RequestProps {
    url: string;
    method?: "GET" | "POST" | "PUT" | "DELETE";
    data?: any;
}

const baseRequest = ({url = "", method = "POST", data = {}}: RequestProps) =>
    new Promise((resolve: CallableFunction, reject: CallableFunction) => {
        http({
            url,
            method,
            data,
        })
            .then((res: any) => {
                const data: ResProps = res.data;
                if (data.success) {
                    resolve(data);
                } else {
                    reject(data);
                }
            })
            .catch(err => {
                reject(err);
            });
    });

export default baseRequest;
